package com.ithome.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ithome.service.SendAliSms;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author sandu
 * @create 2020-06-09 23:09
 */

@Service
public class SendAliSmsImpl implements SendAliSms {

	@Override
	public boolean send(String phoneNum, String templateCode, Map<String, Object> code) {
		//连接阿里云
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GFBakZEgdGpnPRSnLav", "F8oMRcaLNRb6VOvjykrZTYhOCUI3Dg");
		IAcsClient     client  = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setSysMethod(MethodType.POST);
		request.setSysDomain("dysmsapi.aliyuncs.com");
		request.setSysVersion("2017-05-25");
		request.setSysAction("SendSms");

		//自定义
		request.putQueryParameter("PhoneNumbers", phoneNum);
		request.putQueryParameter("SignName", "IThome提示您");
		request.putQueryParameter("TemplateCode", templateCode);
		//构建验证码
		request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));

		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
			return response.getHttpResponse().isSuccess();
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}

		return false;

	}
}
